@echo off
chcp 65001 > nul
setlocal

:prompt

echo "==>TAO_ROOT=%TAO_ROOT%"

:: 创建和联合实时事件通道   使用一个 `TAO_ECG_Mcast_Gateway` 对象封装所有第一种 API 的底层对象。
set PROJECT_PATH="%TAO_ROOT%/orbsvcs/DevGuideExamples/EventServices/RTEC_MCast_Federated"
cd %PROJECT_PATH%


:: 定义要删除的文件列表
set "files=ns.ior ec1.ior ec2.ior"

:: 遍历文件列表并删除存在的文件
for %%f in (%files%) do (
    if exist "%%f" (
        echo Deleting %%f...
        del "%%f"
        if not errorlevel 1 (
            echo Deleted %%f successfully.
        ) else (
            echo Error deleting %%f.
        )
    ) else (
        echo %%f does not exist. Skipping.
    )
)

echo Please select the communication method:
echo 1. Use UDP
echo 2. Use Multicast
set /p choice=Please select the communication method (1 for UDP, 2 for Multicast): 
if "%choice%"=="1" (
    call :use_udp
) else if "%choice%"=="2" (
    call :use_multicast
) else (
    echo 无效的选择，请重新输入。
    goto prompt
)
goto end

::   -----------------------UDP----------------------------
:use_udp
echo Using UDP method
:: 本地事件通道 + CORBA GW 实现发布订阅  udp方式
::1) Start tao_ft_naming
echo "==>Start Naming_Service"
start "tao_cosnaming" cmd /k "%TAO_ROOT%/orbsvcs/Naming_Service/tao_cosnaming -o %PROJECT_PATH%/ns.ior -ORBListenEndpoints iiop://OMEN_2023:18647"
timeout /t 3


::2) Start test object EchoEventSupplier   -ecname ec1本地事件通道; -gateway ec2 远程事件通道
@REM -ORBInitRef NameService=file://%PROJECT_PATH%/ns.ior：指定命名服务的位置。
@REM -ORBSvcConf supplier.conf：指定服务的配置文件。
@REM -ORBListenEndpoints iiop://OMEN_2023：指定ORB监听的端点。
@REM -iorfile %PROJECT_PATH%/ec1.ior：指定IOR文件的路径和名称。
@REM -udp：指定使用UDP通信。
@REM -ecname ec1：指定事件通道的名称为ec1。
@REM -port 20134：指定UDP发送数据的端口号。
@REM -listenport 27566：指定UDP监听数据的端口号。
echo "==>Start EchoEventSupplier create local channel ec1"
start "EchoEventSupplier_ec1" cmd /k "EchoEventSupplier -ORBInitRef NameService=file://%PROJECT_PATH%/ns.ior -ORBSvcConf supplier.conf -ORBListenEndpoints iiop://OMEN_2023 -iorfile %PROJECT_PATH%/ec1.ior -udp -ecname ec1 -port 20134 -listenport 27566"
timeout /t 3


::3) Start test object EchoEventSupplier
echo "==>Start EchoEventSupplier create local channel ec2"
start "EchoEventSupplier_ec2" cmd /k "EchoEventSupplier -ORBInitRef NameService=file://%PROJECT_PATH%/ns.ior -ORBSvcConf supplier.conf -ORBListenEndpoints iiop://OMEN_2023  -iorfile %PROJECT_PATH%/ec2.ior -udp -ecname ec2 -port 27566 -listenport 20134"
timeout /t 3

goto end_0

::   -----------------------use_multicast----------------------------
:use_multicast
echo Using Multicast method
echo 使用多播方式
:: 本地事件通道 + CORBA GW 实现发布订阅  multicast方式

::1) Start tao_ft_naming
echo "==>Start Naming_Service"
start "tao_cosnaming" cmd /k "%TAO_ROOT%/orbsvcs/Naming_Service/tao_cosnaming -o %PROJECT_PATH%/ns.ior -ORBListenEndpoints iiop://OMEN_2023:17713"
timeout /t 3


::2) Start test object EchoEventSupplier   -ecname ec1本地事件通道; -gateway ec2 远程事件通道
echo "==>Start EchoEventSupplier create local channel ec1"
start "EchoEventSupplier_ec1" cmd /k "EchoEventSupplier -ORBInitRef NameService=file://%PROJECT_PATH%/ns.ior -ORBSvcConf supplier.conf -ORBListenEndpoints iiop://OMEN_2023 -iorfile %PROJECT_PATH%/ec1.ior -ecname ec1 -address 224.9.9.2 -port 21695"
timeout /t 3


::3) Start test object EchoEventSupplier
echo "==>Start EchoEventSupplier create local channel ec2"
start "EchoEventSupplier_ec2" cmd /k "EchoEventSupplier -ORBInitRef NameService=file://%PROJECT_PATH%/ns.ior -ORBSvcConf supplier.conf -ORBListenEndpoints iiop://OMEN_2023  -iorfile %PROJECT_PATH%/ec2.ior -ecname ec2 -address 224.9.9.2 -port 21695"
timeout /t 3

goto :end_0

::   ----------------------- EchoEventConsumer ----------------------------
:end_0

::4) Start test object EchoEventConsumer  从命名服务上获取指定通道的使用, -ecname 指定本地事件通道
echo "==>Start EchoEventConsumer look for ec1"
start "EventConLookRemote_ec1" cmd /k "EchoEventConsumer -ORBInitRef NameService=file://%PROJECT_PATH%/ns.ior -ecname ec1 -ORBListenEndpoints iiop://OMEN_2023"
timeout /t 3

::5) Start test object EchoEventConsumer
echo "==>Start EchoEventConsumer look for ec2"
start "EventConLookRemote_ec2" cmd /k "EchoEventConsumer -ORBInitRef NameService=file://%PROJECT_PATH%/ns.ior -ecname ec2 -ORBListenEndpoints iiop://OMEN_2023"
go :end

:end

endlocal
pause